const express = require('express')
const router = express.Router()
const path = require('path')

// 获取验证模块
const authorization = require(path.join(process.cwd(), '/modules/authorization'))

// 通过验证模块获取分类管理
const catServ = require(path.join(process.cwd(), '/services/CategoryService'))
const attrServ = require(path.join(process.cwd(), '/services/AttributeService'))


// 获取分类列表
router.get('/', (req, res) => {
    const conditions = null
    if (req.query.pageNum && req.query.pageSize) {
        conditions = {
            pageNum: req.query.pageNum,
            pageSize: req.query.pageSize
        }
    }
    catServ.getAllCategories(req.query.level, conditions, (err, result) => {
        if(err) return res.sendResult(null,400,"获取分类列表失败")
		res.sendResult(result,200,"获取成功")
    })
})

// 通过参数方式查询动态参数还是静态参数
router.get('/:id/attributes', (req, res) => {
    if (!req.params.id) return res.sendResult(null, 400, '分类ID不能为空')
    if (isNaN(parseInt(req.params.id))) return res.sendResult(null, 400, '分类ID必须是数字')
    if (!req.query.sel || (req.query.sel !== 'only' && req.query.sel !== 'many')) 
    return res.sendResult(null, 400, '属性类型必须设置')

    attrServ.getAttributes(req.params.id, req.query.sel, (err, attrs) => {
        if(err) return res.sendResult(null,400,err)
		res.sendResult(attrs,200,"获取成功")
    })
})

module.exports = router